package com.dq.service.finance.impl;

import com.dq.context.Constant;
import com.dq.exception.customException.AdminException;
import com.dq.controller.in.base.BaseQuery;
import com.dq.controller.in.WorkRoyaltyQuery;
import com.dq.vo.Vo;
import com.dq.vo.baseVo.VO;
import com.dq.domain.base.AbstractEntity;
import com.dq.domain.base.impl.BaseDaoImpl;

import com.dq.domain.org.Org;
import com.dq.domain.position.OrgPosition;
import com.dq.domain.sys.section.SysSection;
import com.dq.domain.setup.WorkRoyaltySet;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.repository.BaseRepositories;
import com.dq.service.finance.WorkRoyaltyService;
import com.dq.service.org.OrgService;
import com.dq.utils.BeanNullUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author luchangtong
 * @Date 2019/12/20 21:00
 */
@Service
public class WorkRoyaltyServiceImpl extends BaseRepositories implements WorkRoyaltyService {

    @Autowired
    private OrgService orgService;
    @Override
    public Vo list(WorkRoyaltyQuery workRoyaltyQuery)  throws AdminException {
        workRoyaltyQuery.format();
        Map<String, Object> query = workRoyaltyQuery.parseMap();
        if(workRoyaltyQuery.getOrgId() != null){
            query.put("org_id",workRoyaltyQuery.getOrgId());
            query.remove("orgId");
        }else {
            // 只显示负责门店信息
            List<Org> orgList = orgService.getListAllByUser();
            if(orgList == null || orgList.size() <= 0) {
                throw new AdminException("此账号无负责门店");
            }
            query.put("org" + BaseDaoImpl.inSqlStr, orgList);
        }
        if (workRoyaltyQuery.getOrgPositionId()!=null){
            query.put("orgPosition_id",workRoyaltyQuery.getOrgPositionId());
            query.remove("orgPositionId");
        }
        if(StringUtils.isNotEmpty(workRoyaltyQuery.getOrgPositionName())){
            query.put("orgPosition_postName"+BaseDaoImpl.likeSqlStr, BaseQuery.likeSql(workRoyaltyQuery.getOrgPositionName()));
            query.remove("orgPositionName");
        }
        if(workRoyaltyQuery.getBeginTime()!=null){
            query.put("createTime"+ BaseDaoImpl.gteSqlStr,workRoyaltyQuery.getBeginTime());
            query.remove("beginTime");
        }
        if(workRoyaltyQuery.getEndTime()!=null){
            query.put("createTime"+BaseDaoImpl.lteSqlStr,workRoyaltyQuery.getEndTime());
            query.remove("endTime");
        }
        List<WorkRoyaltySet> workRoyaltySets = dao.find(dao.query2Hql(WorkRoyaltySet.class,query),query);
        Long count = dao.count(dao.count2Hql(WorkRoyaltySet.class,query),query);
        PageHelper.startPage(workRoyaltyQuery.getPage(),workRoyaltyQuery.getSize());
        PageInfo pageInfo = new PageInfo((List<WorkRoyaltySet.Vo>) VO.toVOCollection(workRoyaltySets));
        pageInfo.setTotal(count);
        return new Vo(pageInfo);
    }

    @Override
    public void add( Long orgId,Long orgPositionId,WorkRoyaltySet workRoyaltySetEdit) {
        workRoyaltySetEdit.setVersion(Constant.VERSION_VALUE);
        workRoyaltySetEdit.setDeleted(false);
        workRoyaltySetEdit.setCreateTime(System.currentTimeMillis());
        workRoyaltySetEdit.setLastModifyTime(System.currentTimeMillis());
        SysUser sysUser = shiroService.getUser();
        workRoyaltySetEdit.setCreateUser(sysUser);
        workRoyaltySetEdit.setUpdateUser(sysUser);
        workRoyaltySetEdit.setRoyaltyNo(AbstractEntity.generyDatetimeAndRandomNum(Constant.ORDERS_SUFF_COUNT));

        SysSection section = dao.get(SysSection.class, orgId);
        OrgPosition orgPosition = dao.get(OrgPosition.class,orgPositionId);
        workRoyaltySetEdit.setOrgPosition(orgPosition);
        workRoyaltySetEdit.setOrg(section);
        workRoyaltySetRepository.saveAndFlush(workRoyaltySetEdit);
    }

    @Override
    public Vo edit( Long orgId,Long orgPositionId,WorkRoyaltySet workRoyaltySetEdit) throws AdminException {
        SysUser sysUser = shiroService.getUser();
        WorkRoyaltySet workRoyaltySet = dao.get(WorkRoyaltySet.class,workRoyaltySetEdit.getId());
        if(workRoyaltySet == null){
            throw new AdminException("请重新选择");
        }
        SysSection section = dao.get(SysSection.class,orgId);
        if(section == null){
            throw new AdminException("请重新选择门店");
        }
        OrgPosition orgPosition = dao.get(OrgPosition.class,orgPositionId);
        if(orgPosition == null){
            throw new AdminException("请重新选择岗位");
        }
        workRoyaltySetEdit.setOrgPosition(orgPosition);
        workRoyaltySetEdit.setOrg(section);
        BeanUtils.copyProperties(workRoyaltySetEdit,workRoyaltySet, BeanNullUtil.getNullPropertyNames(workRoyaltySetEdit));

        workRoyaltySet.setUpdateUser(sysUser);
        workRoyaltySet.setLastModifyTime(System.currentTimeMillis());
        workRoyaltySetRepository.saveAndFlush(workRoyaltySet);
        return new Vo(HttpStatus.OK);
    }

    @Override
    public Vo delete(Long id) throws AdminException {
        SysUser sysUser = shiroService.getUser();
        WorkRoyaltySet workRoyaltySet = dao.get(WorkRoyaltySet.class,id);
        if(workRoyaltySet == null){
            throw new AdminException("请重新选择");
        }
        workRoyaltySet.setLastModifyTime(System.currentTimeMillis());
        workRoyaltySet.setUpdateUser(sysUser);
        workRoyaltySet.setDeleted(true);
        workRoyaltySetRepository.saveAndFlush(workRoyaltySet);
        return new Vo(HttpStatus.OK);
    }
}
